package com.kvest.ula.activities;

import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.TextView;

import com.kvest.ula.android.ColorPickerDialog;
import com.kvest.ula.R;
import com.kvest.ula.core.Settings;

public class SettingsActivity extends ULABaseActivity
{
	private Spinner updateTypeSpinner;
	private Spinner interfaceColorspinner;
	
	private int fontColor;
	private int fontBgColor;
	private SeekBar fontSize;
	
	private TextView textExample;
	
	@Override
    public void onCreate(Bundle savedInstanceState) 
    {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings);
        
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.check_update_type));
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
        updateTypeSpinner = (Spinner) findViewById(R.id.update_type_spinner);
        if (updateTypeSpinner != null)
        {
        	updateTypeSpinner.setAdapter(adapter);   
        	updateTypeSpinner.setSelection(Settings.getSettings().getCheckUpdatePeriod());
        }
        
        ArrayAdapter<String> interfaceColorAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.interface_color));
        interfaceColorAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        interfaceColorspinner = (Spinner) findViewById(R.id.interface_color_spinner);
        if (interfaceColorspinner != null)
        {
        	interfaceColorspinner.setAdapter(interfaceColorAdapter);
        	interfaceColorspinner.setSelection(Settings.getSettings().getInterfaceColor());
        }
        
        Settings settings = Settings.getSettings();
        
        textExample = (TextView)findViewById(R.id.text_example_tv);
        fontSize = (SeekBar)findViewById(R.id.font_size_seekbar);
        fontSize.setProgress(settings.getFontSize());
        fontSize.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() 
        {
			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) 
			{
				setExampleColors();	
			}
			
			public void onStopTrackingTouch(SeekBar seekBar) {}
			public void onStartTrackingTouch(SeekBar seekBar) {}
		});
        
        fontColor = settings.getFontColor();
        fontBgColor = settings.getFontBgColor();
        setExampleColors();
    }
	
	private void setExampleColors()
	{
		textExample.setTextSize(Settings.FONT_SIZE_OFFSET + fontSize.getProgress());
		textExample.setTextColor(fontColor);
		textExample.setBackgroundColor(fontBgColor);
	}
	
	public void onFontColorSelect(View view)
	{
		ColorPickerDialog colorPickerDialog = new ColorPickerDialog(SettingsActivity.this);
		colorPickerDialog.setTitle(getResources().getString(R.string.font_color));
		colorPickerDialog.setListener(new ColorPickerDialog.ColorSelectedListener() 
		{	
			public void colorSelected(Integer color) 
			{
				fontColor = color;
				setExampleColors();
			}
		});
		colorPickerDialog.show();
	}
	
	public void onFontBgColorSelect(View view)
	{
		ColorPickerDialog colorPickerDialog = new ColorPickerDialog(SettingsActivity.this);
		colorPickerDialog.setTitle(getResources().getString(R.string.font_bg_color));
		colorPickerDialog.setListener(new ColorPickerDialog.ColorSelectedListener() 
		{	
			public void colorSelected(Integer color) 
			{
				fontBgColor = color;
				setExampleColors();
			}
		});
		colorPickerDialog.show();
	}
	
	public void onSaveClick(View view)
	{
		Settings settings = Settings.getSettings();
		if (updateTypeSpinner != null)
		{
			settings.setCheckUpdatePeriod(updateTypeSpinner.getSelectedItemPosition());
		}
		
		if (interfaceColorspinner != null)
		{
			settings.setInterfaceColor(interfaceColorspinner.getSelectedItemPosition());
		}
		
		settings.setFontSize(fontSize.getProgress());
		settings.setFontColor(fontColor);
        settings.setFontBgColor(fontBgColor);
		
        settings.save();
        
		finish();
	}
	
	public void onCancelClick(View view)
	{
		finish();
	}
}
